var startKey = "StartStage", endKey = "EndStage";

$(document).ready(function () {
    $("#selectStage").bind("change", function () {
        var stage = $(this).val();
        if (stage) {
            $("#customize").hide();
        } else {
            $("#customize").show();
        }
    });

    $("#submit").bind("click", function () {
        var submitData = {};
        var stage = $("#selectStage").val();
        if (stage) {
            submitData[startKey] = stage;
            submitData[endKey] = $("#latestIdentity").val();
        }
        else {
            submitData[startKey] = $("#startIdentity").val();
            submitData[endKey] = $("#endIdentity").val();
        }

        if ($("#openNewTab").attr("checked")) {
            var url = $("#submitUrl").val() + "?";
            url = url.replace("RetrieveHistoryInfo", "HistoryInfo");
            for (var i in submitData) {
                url += i.toString() + "=" + submitData[i] + "&";
            }
            var nowTime = new Date();
            url += "random=" + nowTime.getDate().toString();
            window.open(url);
        }
        else {
            $.ajax({
                url: $("#submitUrl").val(),
                data: submitData,
                success: function (e) {
                    $("#result").html(e);
                }
            });
        }



    });
});